package com.chinda.modules.job.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.chinda.common.annotation.SysLog;
import com.chinda.common.enums.MessageEnum;
import com.chinda.common.exception.RRException;
import com.chinda.common.utils.R;
import com.chinda.modules.job.dto.ScheduleJobDTO;
import com.chinda.modules.job.entity.ScheduleJobEntity;
import com.chinda.modules.job.qo.ScheduleJobQO;
import com.chinda.modules.job.service.ScheduleJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 定时任务
 *
 * @author chinda
 * @date 2021-07-09 19:09:10
 */
@AllArgsConstructor
@RestController
@RequestMapping("/schedulejobs")
@Api(tags = "定时任务")
public class ScheduleJobController {

    private final ScheduleJobService scheduleJobService;

    /**
     * 分页
     *
     * @param qo
     * @return
     */
    @SysLog("查询定时任务分页")
    @PostMapping("/page")
    @ApiOperation(value = "查询分页信息")
    public R page(@ApiParam(value = "qo", name = "查询实例", required = true) @RequestBody ScheduleJobQO qo) {
        return R.ok().set(scheduleJobService.page(qo));
    }

    /**
     * 获取详情
     *
     * @return
     */
    @SysLog("查询定时任务详情")
    @GetMapping("/{jobId}")
    @ApiOperation(value = "获取详情")
    public R info(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity scheduleJob = scheduleJobService.getById(jobId);
        return R.ok().set(scheduleJob);
    }

    /**
     * 保存
     *
     * @return
     */
    @SysLog("保存定时任务")
    @PostMapping
    @ApiOperation(value = "保存信息")
    public R save(@ApiParam(value = "scheduleJobDTO", name = "定时任务DTO实例", required = true) @RequestBody @Valid ScheduleJobDTO scheduleJobDTO) {
        ScheduleJobEntity scheduleJob = new ScheduleJobEntity();
        BeanUtil.copyProperties(scheduleJobDTO, scheduleJob, CopyOptions.create().ignoreNullValue());
        scheduleJobService.saveJob(scheduleJob);
        return R.ok().set(scheduleJob);
    }

    /**
     * 修改全属性
     *
     * @param scheduleJobDTO
     * @return
     */
    @SysLog("修改定时任务全属性")
    @PutMapping("/{jobId}")
    @ApiOperation(value = "修改全属性")
    public R modify(@PathVariable("jobId") Long jobId,
                    @ApiParam(value = "scheduleJobDTO", name = "定时任务DTO实例", required = true) @RequestBody @Valid ScheduleJobDTO scheduleJobDTO) {
        ScheduleJobEntity scheduleJob = new ScheduleJobEntity();
        BeanUtil.copyProperties(scheduleJobDTO, scheduleJob, CopyOptions.create().ignoreNullValue());
        scheduleJob.setJobId(jobId);
        if (!scheduleJobService.updateJob(scheduleJob)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(scheduleJob);
    }

    /**
     * 修改局部属性
     *
     * @param jobId
     * @param scheduleJobDTO
     * @return
     */
    @SysLog("修改定时任务局部属性")
    @PatchMapping("/{jobId}")
    @ApiOperation(value = "修改局部属性")
    public R patch(@PathVariable("jobId") Long jobId,
                   @ApiParam(value = "scheduleJobDTO", name = "定时任务DTO实例", required = true) @RequestBody @Valid ScheduleJobDTO scheduleJobDTO) {
        ScheduleJobEntity dbScheduleJob = scheduleJobService.getById(jobId);
        if (dbScheduleJob == null) {
            throw new RRException(MessageEnum.SYS_CHECK_0022.getMsg());
        }
        BeanUtil.copyProperties(scheduleJobDTO, dbScheduleJob, CopyOptions.create().ignoreNullValue());
        if (!scheduleJobService.updateJob(dbScheduleJob)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(dbScheduleJob);
    }

    /**
     * 删除数据
     *
     * @param jobId
     * @return
     */
    @SysLog("删除定时任务")
    @DeleteMapping("/{jobId}")
    @ApiOperation(value = "删除数据")
    public R remove(@PathVariable("jobId") Long jobId) {
        scheduleJobService.deleteById(jobId);
        return R.ok();
    }

    /**
     * 批量删除数据
     *
     * @param jobIds
     * @return
     */
    @SysLog("批量删除定时任务")
    @DeleteMapping("/batch")
    @ApiOperation(value = "批量删除数据")
    public R remove(@ApiParam(value = "jobIds", name = "jobId集合", required = true) @RequestBody Long[] jobIds) {
        scheduleJobService.deleteBatch(Arrays.asList(jobIds));
        return R.ok();
    }

    /**
     * 立即执行任务
     * @param jobIds
     * @return
     */
    @SysLog("立即执行任务")
    @RequestMapping("/run")
    public R run(@ApiParam(value = "jobIds", name = "jobId集合", required = true) @RequestBody Long[] jobIds){
        scheduleJobService.run(jobIds);
        return R.ok();
    }

    /**
     * 暂停定时任务
     * @param jobIds
     * @return
     */
    @SysLog("暂停定时任务")
    @RequestMapping("/pause")
    public R pause(@ApiParam(value = "jobIds", name = "jobId集合", required = true) @RequestBody Long[] jobIds){
        scheduleJobService.pause(jobIds);
        return R.ok();
    }

    /**
     * 恢复定时任务
     * @param jobIds
     * @return
     */
    @SysLog("恢复定时任务")
    @RequestMapping("/resume")
    public R resume(@ApiParam(value = "jobIds", name = "jobId集合", required = true) @RequestBody Long[] jobIds){
        scheduleJobService.resume(jobIds);
        return R.ok();
    }
}
